(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("assetrepaymentService", assetrepaymentService)
	.filter("paySrc", [ '$filter', '$locale', 'assetrepaymentService', function($filter, $locale, assetrepaymentService) {
		return function(paySrc, pattern) {
			return $filter('translate')(assetrepaymentService.paySrcFilter(paySrc, pattern));
		};
	} ])
	.filter("rolloverFlag", [ '$filter', '$locale', 'assetrepaymentService', function($filter, $locale, assetrepaymentService) {
		return function(rolloverFlag, pattern) {
			return $filter('translate')(assetrepaymentService.rolloverFlagsFilter(rolloverFlag, pattern));
		};
	} ])
	.filter("assetrepayStatus", [ '$filter', '$locale', 'assetrepaymentService', function($filter, $locale, assetrepaymentService) {
		return function(assetrepayStatus, pattern) {
			return $filter('translate')(assetrepaymentService.assetrepayStatusFilter(assetrepayStatus, pattern));
		};
	} ]);
	assetrepaymentService.$inject = [ '$q', 'loggerFactory', 'ajaxServiceFactory' ];
	function assetrepaymentService($q, loggerFactory, ajaxServiceFactory) {
		var log = loggerFactory.getLogger("assetrepaymentService");
		var ajaxServices = ajaxServiceFactory.buildAjaxService({
			getAssetrepaymentListOptions: {
				url: 'do/apf/dataList/getAssetrepaymentListOptions',
				method: 'get'
			},
			getAssetrepaymentViewOptions: {
				url: 'do/apf/dataList/getAssetrepaymentViewOptions',
				method: 'get'
			}
		});	
		
		var _assetrepaymentStatus = null,_paySrc = null, _yesOrNo = null;
		
		function getAssetrepaymentListOptions() {
			var defer = $q.defer();
			ajaxServices.getAssetrepaymentListOptions().then(function(response) {
				_assetrepaymentStatus = response.data.assetrepaymentStatus;
				_yesOrNo = response.data.yesOrNo;
				_paySrc = response.data.paySrc;
				defer.resolve(response.data);
			});
			return defer.promise;
		}
		
		function getAssetrepaymentViewOptions() {
			var defer = $q.defer();
			ajaxServices.getAssetrepaymentViewOptions().then(function(response) {
				_assetrepaymentStatus = response.data.assetrepaymentStatus;
				defer.resolve(response.data);
			});
			return defer.promise;
		}
		
		function paySrcFilter(optionValue) {
			var optionLabel = '';
			if(_paySrc) {
				angular.forEach(_paySrc, function(obj, index) {
					if(optionValue == obj.value) {
						optionLabel = obj.label;
					}
				});
			}
			return optionLabel;
		}
		
		function rolloverFlagsFilter(optionValue) {
			var optionLabel = '';
			if(_yesOrNo) {
				angular.forEach(_yesOrNo, function(obj, index) {
					if(optionValue == obj.value) {
						optionLabel = obj.label;
					}
				});
			}
			return optionLabel;
		}
		
		function assetrepayStatusFilter(optionValue) {
			var optionLabel = '';
			if(_assetrepaymentStatus) {
				angular.forEach(_assetrepaymentStatus, function(obj, index) {
					if(optionValue == obj.value) {
						optionLabel = obj.label;
					}
				});
			}
			return optionLabel;
		}
		
		return {
			getAssetrepaymentListOptions : getAssetrepaymentListOptions,
			getAssetrepaymentViewOptions : getAssetrepaymentViewOptions,
			paySrcFilter : paySrcFilter,
			rolloverFlagsFilter : rolloverFlagsFilter,
			assetrepayStatusFilter : assetrepayStatusFilter
		};
	}
})(angular);